MACRO(SETUP_JAVALIB JAVALIB_NAME)
	ADD_LIBRARY(${JAVALIB_NAME} ${SOURCES})
	INSTALL(TARGETS ${JAVALIB_NAME}
		ARCHIVE DESTINATION lib
	)
	ENDIF(WIN32)
ENDMACRO(SETUP_JAVALIB)

MACRO(SETUP_CORELIB CORELIB_NAME)

	ADD_LIBRARY(${CORELIB_NAME}
		SHARED
		${HEADERS}
		${SOURCES}
	)

	TARGET_LINK_LIBRARIES(${CORELIB_NAME} ${LIBRARIES})

	IF(LIBRARIES_OPTIMIZED)
		FOREACH(LIBOPT ${LIBRARIES_OPTIMIZED})
			TARGET_LINK_LIBRARIES(${CORELIB_NAME} optimized ${LIBOPT})
		ENDFOREACH(LIBOPT)
	ENDIF(LIBRARIES_OPTIMIZED)

	IF(LIBRARIES_DEBUG)
		FOREACH(LIBDEBUG ${LIBRARIES_DEBUG})
			TARGET_LINK_LIBRARIES(${CORELIB_NAME} debug ${LIBDEBUG})
		ENDFOREACH(LIBDEBUG)
	ENDIF(LIBRARIES_DEBUG)

	SET_TARGET_PROPERTIES(${CORELIB_NAME}
		PROPERTIES
		PROJECT_LABEL "Core ${CORELIB_NAME}"
	)

	IF(WIN32)
		INSTALL(TARGETS ${CORELIB_NAME}
			RUNTIME DESTINATION .
			#ARCHIVE DESTINATION lib
		)
	ELSE(WIN32)
		INSTALL(TARGETS ${CORELIB_NAME}
			LIBRARY DESTINATION .
		)
	ENDIF(WIN32)

	IF(RESOURCE_FILES)
		FOREACH(RESOURCE ${RESOURCE_FILES})
			GET_FILENAME_COMPONENT(RESOURCE_NAME ${RESOURCE} NAME)
			ADD_CUSTOM_COMMAND(TARGET ${CORELIB_NAME} POST_BUILD
				COMMAND ${CMAKE_COMMAND} 
           		ARGS    -E copy ${RESOURCE} ${CMAKE_INSTALL_PREFIX}
           		COMMENT "Copying ${RESOURCE_NAME} to ${CMAKE_INSTALL_PREFIX}"
			)
		ENDFOREACH(RESOURCE ${RESOURCE_FILES})
	ENDIF(RESOURCE_FILES)

	IF(UNIX AND NOT APPLE)
		SET_TARGET_PROPERTIES ( ${LIB_NAME} PROPERTIES LINK_FLAGS "-Wl,-E")
	ENDIF(UNIX AND NOT APPLE)

ENDMACRO(SETUP_CORELIB)

MACRO(SETUP_JNILIB JNILIB_NAME)

	ADD_LIBRARY(${JNILIB_NAME}
		SHARED
		${HEADERS}
		${SOURCES}
	)

	TARGET_LINK_LIBRARIES(${JNILIB_NAME} ${LIBRARIES})

	IF(LIBRARIES_OPTIMIZED)
		FOREACH(LIBOPT ${LIBRARIES_OPTIMIZED})
			TARGET_LINK_LIBRARIES(${JNILIB_NAME} optimized ${LIBOPT})
		ENDFOREACH(LIBOPT)
	ENDIF(LIBRARIES_OPTIMIZED)

	IF(LIBRARIES_DEBUG)
		FOREACH(LIBDEBUG ${LIBRARIES_DEBUG})
			TARGET_LINK_LIBRARIES(${JNILIB_NAME} debug ${LIBDEBUG})
		ENDFOREACH(LIBDEBUG)
	ENDIF(LIBRARIES_DEBUG)
	
	IF(APPLE)
		SET_TARGET_PROPERTIES(${LIB_NAME}
    	PROPERTIES
    	SUFFIX .jnilib)
	ENDIF(APPLE)
	

	SET_TARGET_PROPERTIES(${JNILIB_NAME}
		PROPERTIES
		PROJECT_LABEL "JNI ${JNILIB_NAME}"
	)

	IF(WIN32)
		INSTALL(TARGETS ${JNILIB_NAME}
			RUNTIME DESTINATION bin
			#ARCHIVE DESTINATION lib
		)
	ELSE(WIN32)
		INSTALL(TARGETS ${JNILIB_NAME}
			LIBRARY DESTINATION lib
		)
	ENDIF(WIN32)

	IF(RESOURCE_FILES)
		FOREACH(RESOURCE ${RESOURCE_FILES})
			GET_FILENAME_COMPONENT(RESOURCE_NAME ${RESOURCE} NAME)
			ADD_CUSTOM_COMMAND(
				TARGET ${JNILIB_NAME} POST_BUILD
				COMMAND ${CMAKE_COMMAND} 
           		ARGS    -E copy ${RESOURCE} ${CMAKE_INSTALL_PREFIX}
           		COMMENT "Copying ${RESOURCE_NAME} to ${CMAKE_INSTALL_PREFIX}"
			)
		ENDFOREACH(RESOURCE ${RESOURCE_FILES})
	ENDIF(RESOURCE_FILES)


	IF(UNIX AND NOT APPLE)
		SET_TARGET_PROPERTIES (${LIB_NAME} PROPERTIES LINK_FLAGS "-Wl,-E")
	ENDIF(UNIX AND NOT APPLE)

ENDMACRO(SETUP_JNILIB)

MACRO(SETUP_CONSOLE_EXAMPLE EXAMPLE_NAME)

	ADD_EXECUTABLE(${EXAMPLE_NAME}
		${HEADERS}
		${SOURCES}
	)
	TARGET_LINK_LIBRARIES(${EXAMPLE_NAME} ${LIBRARIES})
	IF(LIBRARIES_OPTIMIZED)
		FOREACH(LIBOPT ${LIBRARIES_OPTIMIZED})
			TARGET_LINK_LIBRARIES(${EXAMPLE_NAME} optimized ${LIBOPT})
		ENDFOREACH(LIBOPT)
	ENDIF(LIBRARIES_OPTIMIZED)

	IF(LIBRARIES_DEBUG)
		FOREACH(LIBDEBUG ${LIBRARIES_DEBUG})
			TARGET_LINK_LIBRARIES(${EXAMPLE_NAME} debug ${LIBDEBUG})
		ENDFOREACH(LIBDEBUG)
	ENDIF(LIBRARIES_DEBUG)

	IF(RESOURCE_FILES)
		FOREACH(RESOURCE ${RESOURCE_FILES})
			GET_FILENAME_COMPONENT(RESOURCE_NAME ${RESOURCE} NAME)
			ADD_CUSTOM_COMMAND(
				TARGET ${EXAMPLE_NAME} POST_BUILD
				COMMAND ${CMAKE_COMMAND} 
           		ARGS    -E copy ${RESOURCE} ${CMAKE_INSTALL_PREFIX}
           		COMMENT "Copying ${RESOURCE_NAME} to ${CMAKE_INSTALL_PREFIX}"
			)
		ENDFOREACH(RESOURCE ${RESOURCE_FILES})
	ENDIF(RESOURCE_FILES)


	SET_TARGET_PROPERTIES(${EXAMPLE_NAME} PROPERTIES PROJECT_LABEL "Example ${EXAMPLE_NAME}")

ENDMACRO(SETUP_CONSOLE_EXAMPLE)

MACRO(SETUP_DEMO DEMO_NAME)

	ADD_EXECUTABLE(${DEMO_NAME}
		${HEADERS}
		${SOURCES}
	)
	TARGET_LINK_LIBRARIES(${DEMO_NAME} ${LIBRARIES})
	IF(LIBRARIES_OPTIMIZED)
		FOREACH(LIBOPT ${LIBRARIES_OPTIMIZED})
			TARGET_LINK_LIBRARIES(${DEMO_NAME} optimized ${LIBOPT})
		ENDFOREACH(LIBOPT)
	ENDIF(LIBRARIES_OPTIMIZED)

	IF(LIBRARIES_DEBUG)
		FOREACH(LIBDEBUG ${LIBRARIES_DEBUG})
			TARGET_LINK_LIBRARIES(${DEMO_NAME} debug ${LIBDEBUG})
		ENDFOREACH(LIBDEBUG)
	ENDIF(LIBRARIES_DEBUG)

	SET_TARGET_PROPERTIES(${DEMO_NAME} PROPERTIES PROJECT_LABEL "Demo ${DEMO_NAME}")

	INSTALL(TARGETS ${DEMO_NAME}
		RUNTIME DESTINATION .
	)

	IF(RESOURCE_FILES)
		FOREACH(RESOURCE ${RESOURCE_FILES})
			GET_FILENAME_COMPONENT(RESOURCE_NAME ${RESOURCE} NAME)
			ADD_CUSTOM_COMMAND(
				TARGET ${DEMO_NAME} POST_BUILD
				COMMAND ${CMAKE_COMMAND} 
           		ARGS    -E copy ${RESOURCE} ${CMAKE_INSTALL_PREFIX}
           		COMMENT "Copying ${RESOURCE_NAME} to ${CMAKE_INSTALL_PREFIX}"
			)
		ENDFOREACH(RESOURCE ${RESOURCE_FILES})
	ENDIF(RESOURCE_FILES)
	
ENDMACRO(SETUP_DEMO)

MACRO(FIND_OSGPLUGIN_LIBRARY MYOSGPLUGIN MYOSGPLUGINNAME)

    FIND_LIBRARY(${MYOSGPLUGIN}
        NAMES osgdb_${MYOSGPLUGINNAME}
        PATHS
        $ENV{OSG_DIR}/lib
        $ENV{OSG_DIR}
        $ENV{OSGDIR}/lib
        $ENV{OSGDIR}
        $ENV{OSG_ROOT}/lib
        ~/Library/Frameworks
        /Library/Frameworks
        /usr/local/lib
        /usr/lib
        /sw/lib
        /opt/local/lib
        /opt/csw/lib
        /opt/lib
        [HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session\ Manager\\Environment;OSG_ROOT]/lib
        /usr/freeware/lib64
    )

ENDMACRO(FIND_OSGPLUGIN_LIBRARY MYOSGPLUGIN MYOSGPLUGINNAME)


